<?php

namespace app\api\controller;

use app\admin\model\News as NewsModel;
use app\common\controller\Api;

/**
 * 新闻接口
 */
class News extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 新闻
     *
     */
    public function index()
    {
        $newId = input('id/d');
        $this->success('获取成功', [
            'news' => $this->news($newId),     //获取新闻
        ], 1);
    }

    /**
     * 新闻
     *
     */
    public function news($newId)
    {
        $model = new NewsModel();
        if (empty($newId)) {
            $list = $model->where(['status' => 1])
                ->field('id,title,image,content,read_num')
                ->limit(0, 5)
                ->select();
            foreach ($list as &$v) {
                $v['image'] = strtolower(config("url_domain_root") . str_replace('\\', '/', $v['image']));
            }
            return $list;
        } else {
            $detail = $model->where(['status' => 1, 'id' => $newId])
                ->field('id,title,content,read_num')
                ->find();
            return $detail;
        }
    }
}
